from flask import jsonify, request

from db.dao import StockHoldersDao, StockLifelineDao, StockOptionalDao, StockPoolDao, HoldersRecordDao
from db.models import StockHolders, StockOptional, HoldersRecord, StockPool
from service.requests.Eastmoney import Eastmoney
from service.stock.lifeline import Lifeline
from utils.util import FlaskTool, StockTool, ObjDictTool, CommonUtil
from web import custom
from datetime import datetime


@custom.route('/optional/pool/add')
def addOptionalFromPool():
    '''
    新增自选股
    :return:
    '''
    pool_id = request.args.get('id', type=int)
    stock_pool = StockPoolDao().queryPoolById(pool_id)

    if stock_pool:
        optional = StockOptional()
        ObjDictTool.copyA2B(stock_pool, optional)
        setattr(optional, 'create_time', datetime.now())
        setattr(optional, 'hold_state', 0)
        setattr(optional, 'line_state', 0)

        StockOptionalDao().saveOrUpdate(optional)

        setattr(stock_pool, 'optional_state', 1)
        StockPoolDao().saveOrUpdate(stock_pool)
    return 'ok'

@custom.route('/optional/remove')
def optional_remove():
    '''
    移除自选股
    :return:
    '''
    stock_code = request.args.get('stock_code', type=str)

    if stock_code:
        StockOptionalDao().removeOptional(stock_code)

        stock_pool=StockPool()
        setattr(stock_pool,'stock_code',stock_code)
        setattr(stock_pool, 'optional_state', 0)
        StockPoolDao().saveOrUpdate(stock_pool)
    return 'ok'


@custom.route('/optional/stock-list')
def stockOptionalList():
    '''
    持股列表
    :return:
    '''
    pagination = StockOptionalDao().queryByPage(request.args)
    return jsonify(FlaskTool.paginatioToList(pagination))


@custom.route('/optional/create_lifelines')
def optional_lifeline():
    '''
    生成生命线
    :return:
    '''
    stock_code = request.args.get('stock_code', type=str)
    stock_name = request.args.get('stock_name', type=str)
    # 调用生成生命线的接口
    stock_pd = Eastmoney.get_all_klines(stock_code)
    stock_pd = stock_pd.astype('float')
    stock_pd.dtypes
    Lifeline(stock_pd, stock_code, stock_name).create_line()

    stock_optional=StockOptional()
    setattr(stock_optional,'stock_code',stock_code)
    setattr(stock_optional,'line_state',1)
    StockOptionalDao().saveOrUpdate(stock_optional)

    return 'ok'


@custom.route('/optional/buy-stock', methods=['POST'])
def optional_buy_stock():
    '''
    买入股票信息
    :return:
    '''
    # 保存持股信息
    optional_id = request.form.get('optional_id')
    optional = StockOptionalDao().queryOptionalById(optional_id)
    if optional:
        if getattr(optional, 'hold_state') == 1:
            return '已经持有该股，请从持有股列表里追加购买'

        stock_code = getattr(optional, 'stock_code')
        stock_name = getattr(optional, "stock_name")
        buy_price = request.form.get('buy_price')
        buy_nums = request.form.get('buy_nums')
        buy_time = request.form.get('buy_time')

        handle_amount = buy_nums * buy_price
        # 保存持股信息
        stockHolders = StockHolders()
        holder_code = CommonUtil.get_uuid()
        setattr(stockHolders, 'holder_code', holder_code)

        setattr(stockHolders, 'stock_code', stock_code)
        setattr(stockHolders, 'stock_name', stock_name)
        setattr(stockHolders, 'market', StockTool.code2Market(stock_code))
        setattr(stockHolders, 'average_price', buy_price)
        setattr(stockHolders, 'cur_nums', buy_nums)
        setattr(stockHolders, 'buy_time', buy_time)
        StockHoldersDao().saveOrUpdate(stockHolders)

        # 保存持股记录
        holders_record = HoldersRecord()
        setattr(holders_record, 'holder_code', holder_code)
        setattr(holders_record, 'stock_name', stock_name)
        setattr(holders_record, 'stock_code', stock_code)
        setattr(holders_record, 'handle_nums', buy_nums)
        setattr(holders_record, 'handle_price', buy_price)
        setattr(holders_record, 'handle_amount', handle_amount)
        setattr(holders_record, 'remain_nums', buy_nums)
        setattr(holders_record, 'handle_time', buy_time)

        HoldersRecordDao().save(holders_record)

        # 更细自选股里购买状态
        setattr(optional, 'hold_state', 1)
        StockOptionalDao.saveOrUpdate(optional)
